//
//  GQCityController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/9.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQCityController.h"
#import "GQAreaInfo.h"
#import "GQCitiesInfo.h"
#import "GQTabBar.h"
#import "GQCityFriendController.h"
#define kCityMargin 10

@interface GQCityController () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) UIView *locationView;
@property (weak, nonatomic) UIView *collFriendsView;
@property (weak, nonatomic) UIView *hotStatusView;
@property (strong, nonatomic) NSArray *areaInfo;
@property (assign, nonatomic) NSInteger indexOfProvince;
@property (assign, nonatomic) NSInteger indexOfCity;
@property (strong, nonatomic) UIView *coverView;
@property (weak, nonatomic) UIView *navCoverView;
@property (weak, nonatomic) UIPickerView *pickerView;
@property (weak, nonatomic) UIView *pickerToolView;
@property (weak, nonatomic) UILabel *locationLabel;
@property (copy, nonatomic) NSString *oneComStr;
@property (copy, nonatomic) NSString *twoComStr;
@property (copy, nonatomic) NSString *threeComStr;

@end

@implementation GQCityController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(self.view.width, 800);
    self.scrollView = scrollView;
    
    [self setUpCurrentLocationView];
    [self setUpCollectionFriends];
    [self setUpHotStatusView];
    [self setUpHotLabelView];
    

}

- (NSArray *)areaInfo
{
    if (_areaInfo == nil) {
        _areaInfo = [GQAreaInfo areaInfoWithDict];
    }
    return _areaInfo;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    GQAreaInfo *areaInfo = self.areaInfo[_indexOfProvince];
    GQCitiesInfo *citiesInfo = areaInfo.cities[_indexOfCity];
    if (component == 0) {
        return self.areaInfo.count;
    }else if(component == 1){
        return areaInfo.cities.count;
    }if (component == 2) {
        return citiesInfo.areas.count;
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.indexOfProvince = row;
        self.indexOfCity = 0;
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }else if(component == 1){
        self.indexOfCity = row;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
}
//
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return 60;
    }else if(component == 1){
        return 80;
    }else if(component == 2){
        return 180;
    }
    return 0;
        
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    if (component == 0) {
        GQAreaInfo *areaInfo = self.areaInfo[row];
        myView = [[UILabel alloc] initWithFrame:CGRectMake(-10, 0, 60, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.font = [UIFont systemFontOfSize:13];
        myView.text = areaInfo.state;
        self.oneComStr = myView.text;
        myView.backgroundColor = [UIColor clearColor];
    }else if(component == 1){
        GQAreaInfo *areaInfo = self.areaInfo[_indexOfProvince];
        GQCitiesInfo *citiesInfo = areaInfo.cities[row];
        myView = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, 80, 30)];
        myView.text = citiesInfo.city;
        self.twoComStr = myView.text;
        myView.textAlignment = NSTextAlignmentCenter;
        myView.font = [UIFont systemFontOfSize:13];
        myView.backgroundColor = [UIColor clearColor];
    }else if(component == 2){
        GQAreaInfo *areaInfo = self.areaInfo[_indexOfProvince];
        GQCitiesInfo *citiesInfo = areaInfo.cities[_indexOfCity];
        myView = [[UILabel alloc] initWithFrame:CGRectMake(-10, 0, 180, 30)];
        myView.text = citiesInfo.areas[row];
        self.threeComStr = myView.text;
        myView.textAlignment = NSTextAlignmentCenter;
        myView.font = [UIFont systemFontOfSize:13];
        myView.backgroundColor = [UIColor clearColor];
    }
    return myView;
}

- (void)setUpCurrentLocationView
{
    UIView *locationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kCurrentWidth, 35)];
    locationView.backgroundColor = [UIColor whiteColor];
    UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, locationView.height)];
    locationLabel.font = [UIFont systemFontOfSize:13];
    locationLabel.text = @"  当前位置浙江";
    self.locationLabel = locationLabel;
    UIImageView *rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_arrow"]];
    rightImage.frame = CGRectMake(locationView.width - 30, (locationView.height - 22) / 2, 22, 22);
    [locationView addSubview:locationLabel];
    [locationLabel addSubview:rightImage];
    [self.scrollView addSubview:locationView];
    self.locationView = locationView;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDidClick)];
    [locationView addGestureRecognizer:tapGestureRecognizer];
}

- (void)tapGestureDidClick
{
    [GQTabBar sharedGQTabBar].hidden = YES;
    
    self.coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.coverView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.coverView];
    UITapGestureRecognizer *tapGestureRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deallocPickerView)];
    [self.coverView addGestureRecognizer:tapGestureRecoginzer];

    UIView *coverViewNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kNavigatationBarHeight - 20)];
    coverViewNav.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar addSubview:coverViewNav];
    UITapGestureRecognizer *tapGestureRecoginzer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deallocPickerView)];
    [coverViewNav addGestureRecognizer:tapGestureRecoginzer1];
    self.navCoverView = coverViewNav;
    
    CGFloat pickerHeight = 150;
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.height - pickerHeight - kToolBarHeight, self.view.width, pickerHeight)];
    pickerView.backgroundColor = [UIColor orangeColor];
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = NO;
    self.pickerView = pickerView;
    
    CGFloat height = 35;
    UIView *pickerToolBar = [[UIView alloc] initWithFrame:CGRectMake(0, pickerView.y - height, pickerView.width, height)];
    pickerToolBar.backgroundColor = [UIColor blueColor];
    self.pickerToolView = pickerToolBar;
    //完成按钮
    UIButton *finishBtn = [[UIButton alloc] initWithFrame:CGRectMake(pickerToolBar.width - 44, 0, 40, pickerToolBar.height)];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    finishBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [finishBtn addTarget:self action:@selector(finishBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [pickerToolBar addSubview:finishBtn];
    //取消按钮
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(4, 0, 40, height)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [cancelBtn addTarget:self action:@selector(cancelBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [pickerToolBar addSubview:cancelBtn];
    [self.view addSubview:pickerToolBar];
    
    [self.view addSubview:pickerView];
    
    
}

- (void)finishBtnDidClick
{
    [self deallocPickerView];
    self.locationLabel.text = [NSString stringWithFormat:@"  当前位置%@ %@ %@", self.oneComStr, self.twoComStr, self.threeComStr];
}

- (void)cancelBtnDidClick
{
    [self deallocPickerView];
}

- (void)deallocPickerView
{
    [GQTabBar sharedGQTabBar].hidden = NO;
    [self.coverView removeFromSuperview];
    [self.navCoverView removeFromSuperview];
    [self.pickerView removeFromSuperview];
    [self.pickerToolView removeFromSuperview];
    self.coverView = nil;
    self.navCoverView = nil;
    self.pickerView = nil;
    self.pickerToolView = nil;
}

- (void)setUpCollectionFriends
{
    UIView *collFriendsView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.locationView.frame) + 10, self.view.width, 105)];
    collFriendsView.backgroundColor = [UIColor whiteColor];
    self.collFriendsView = collFriendsView;
    [self.scrollView addSubview:collFriendsView];
    
    //标签头
    UILabel *titleImage = [[UILabel alloc] initWithFrame:CGRectMake(7, 12, 10, 12)];
    titleImage.backgroundColor = [UIColor blueColor];
    [collFriendsView addSubview:titleImage];
    
    //标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleImage.frame) + 5, titleImage.y, 60, 12)];
    title.text = @"人气藏友";
    title.font = [UIFont systemFontOfSize:12];
    [collFriendsView addSubview:title];
    //更多
    UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(collFriendsView.width - 45, title.y, 40, 12)];
    moreBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [moreBtn setTitle:@"更多》" forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(moreBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [collFriendsView addSubview:moreBtn];
    
    NSArray *nameArr = [NSArray arrayWithObjects:@"李小龙", @"张明", @"林志颖", @"张明德", @"张雪", nil];
    
    CGFloat margin = 10;
    CGFloat width = (collFriendsView.width - margin * 6) / 5;
    CGFloat y = CGRectGetMaxY(title.frame) + 10;
    CGFloat height = width + 30;
    
    for (int i = 0; i < 5; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.x = margin + (width + margin) * i;
        btn.y = y;
        btn.width = width;
        btn.height = height;
        [collFriendsView addSubview:btn];
        
        //头像
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithRoundName:@"icon"]];
        imageView.frame = CGRectMake(0, 0, btn.width, btn.width);
        [btn addSubview:imageView];
        
        //姓名
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame) + 10, btn.width, 20)];
        nameLabel.font = [UIFont systemFontOfSize:12];
        nameLabel.text = nameArr[i];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:nameLabel];
    }
    collFriendsView.height = y + height;
}

- (void)moreBtnDidClick
{
    GQCityFriendController *CGFC = [[GQCityFriendController alloc] init];
    [self.navigationController pushViewController:CGFC animated:YES];
}

- (void)setUpHotStatusView
{
    
    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.collFriendsView.frame) + 10, self.view.width, 0)];
    statusView.backgroundColor = [UIColor whiteColor];
    self.hotStatusView = statusView;
    [self.scrollView addSubview:statusView];
    
    //标签头
    UILabel *titleImage = [[UILabel alloc] initWithFrame:CGRectMake(7, 12, 10, 12)];
    titleImage.backgroundColor = [UIColor greenColor];
    [statusView addSubview:titleImage];
    
    //标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleImage.frame) + 5, titleImage.y, 60, 12)];
    title.text = @"热门状态";
    title.font = [UIFont systemFontOfSize:12];
    [statusView addSubview:title];
    //更多
    UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(statusView.width - 45, title.y, 40, 12)];
    moreBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [moreBtn setTitle:@"更多》" forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [statusView addSubview:moreBtn];
    
    NSInteger count = 3;
    CGFloat imageWidth = 200;
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.x = kCityMargin;
    scrollView.y = CGRectGetMaxY(titleImage.frame) + 10;
    scrollView.width = statusView.width - 2 * kCityMargin;
    scrollView.height = 150;
    scrollView.bounces = NO;
    scrollView.contentSize = CGSizeMake(imageWidth * count, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    [statusView addSubview:scrollView];
    
    
    for (int i = 0; i < 3; i++) {
        UIButton *btnImage = [[UIButton alloc] init];
        btnImage.x = i * (5 + imageWidth);
        btnImage.y = 0;
        btnImage.width = imageWidth;
        btnImage.height = scrollView.height;
        [btnImage setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
        [scrollView addSubview:btnImage];
        
        UIView *coverView = [[UIView alloc] initWithFrame:btnImage.bounds];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.5;
        [btnImage addSubview:coverView];
        
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageWithRoundName:@"icon"]];
        icon.x = 20;
        icon.y = 20;
        icon.width = 25;
        icon.height = 25;
        [btnImage addSubview:icon];
        
        UILabel *commentLabel = [[UILabel alloc] init];
        commentLabel.x = icon.x;
        commentLabel.y = CGRectGetMaxY(icon.frame) + 5;
        commentLabel.width = btnImage.width - 2 * 20;
        commentLabel.height = 20;
        commentLabel.font = [UIFont systemFontOfSize:15];
        commentLabel.text = @"昨天正好收了一个";
        commentLabel.textColor = [UIColor whiteColor];
        [btnImage addSubview:commentLabel];
        
    }
    statusView.height = CGRectGetMaxY(scrollView.frame) + 10;
    
}

- (void)setUpHotLabelView
{
    UIView *hotLabelView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.hotStatusView.frame) + 10, self.view.width, 105)];
    hotLabelView.backgroundColor = [UIColor whiteColor];
    //self.hotLabelView = hotLabelView;
    [self.scrollView addSubview:hotLabelView];
    
    //标签头
    UILabel *titleImage = [[UILabel alloc] initWithFrame:CGRectMake(7, 12, 10, 12)];
    titleImage.backgroundColor = [UIColor greenColor];
    [hotLabelView addSubview:titleImage];
    
    //标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleImage.frame) + 5, titleImage.y, 60, 12)];
    title.text = @"热门标签";
    title.font = [UIFont systemFontOfSize:12];
    [hotLabelView addSubview:title];
    
    NSArray *labelArr = [NSArray arrayWithObjects:@"细化雕塑", @"气势上市", @"木器家具", @"细化雕塑", @"气势上市", @"木器家具",@"细化雕塑", @"气势上市", @"木器家具", nil];
    NSInteger count = labelArr.count;
    CGFloat margin = 10;
    CGFloat width = (hotLabelView.width - 4 * 10) / 3;
    CGFloat height = width / 4 * 3;
    CGFloat y = CGRectGetMaxY(titleImage.frame) + 5;
    
    for (int i = 0; i < count; i++) {
        UIButton *btnLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLabel.x = margin + (margin + width) * (i % 3);
        btnLabel.y = y + (margin + height) * (i / 3);
        btnLabel.width = width;
        btnLabel.height = height;
        [btnLabel setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
        
        UIView *coverView = [[UIView alloc] initWithFrame:btnLabel.bounds];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.5;
        [btnLabel addSubview:coverView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.width = 60;
        titleLabel.height = 20;
        titleLabel.x = (btnLabel.width - titleLabel.width) / 2;
        titleLabel.y = (btnLabel.height - titleLabel.height) / 2;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = labelArr[i];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [btnLabel addSubview:titleLabel];
        [hotLabelView addSubview:btnLabel];
    }
    hotLabelView.height = CGRectGetMaxY(titleImage.frame) + (height + margin) * (count / 3) + 5;
    CGFloat h = self.locationView.height + self.collFriendsView.height + self.hotStatusView.height + height + hotLabelView.height + 73;
       self.scrollView.contentSize = CGSizeMake(self.view.width, h);
    
}


@end
